//
// Created by xili on 2024/9/2 16:39.
// GO BIG OR GO HOME.
//
#include "leetcode.h"

class Solution {
public:
    int largestValsFromLabels(vector<int> &values, vector<int> &labels, int numWanted, int useLimit) {
        int n = values.size();
        unordered_map<int, int> m;
        for (int label: labels) {
            m[label] = useLimit;
        }
        priority_queue <pair<int, int>> q;
        for (int i = 0; i < n; i++) {
            q.push({values[i], labels[i]});
        }
        int res = 0;
        for (int i = 0; i < numWanted; i++) {
            while (!q.empty() && m[q.top().second] == 0) {
                q.pop();
            }
            if (q.empty()) return res;
            res += q.top().first;
            m[q.top().second]--;
            q.pop();
        }
        return res;
    }
};